﻿#region Using
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion Using

namespace LloydGame
{
    class ScreenMultiplayer : GameScreen
    {

        #region Fields
        protected GameScreenComponent m_menu_QuickMatch,
                                      m_menu_LIVEMatch,
                                      m_menu_Back;
        protected String m_lasterror;
        #endregion Fields

        #region Properties
        public String lasterror
        {
            get { return m_lasterror; }
            set { m_lasterror = value; }
        }
        #endregion Properties

        #region Constructor
        public ScreenMultiplayer(Game p_game, GameScreen p_owner)
            : base(p_game, "Multiplayer")
        {
            m_menu_QuickMatch = new GameScreenComponent("Quick Match", new Vector2(80, 200), new Vector2(240, 32), p_game);
            m_menu_LIVEMatch = new GameScreenComponent("LIVE Game", new Vector2(80, 240), new Vector2(240, 32), p_game);
            m_menu_Back = new GameScreenComponent("Back", new Vector2(80, 280), new Vector2(240, 32), p_game);

            m_selectable_components.Add(m_menu_QuickMatch);
            m_selectable_components.Add(m_menu_LIVEMatch);
            m_selectable_components.Add(m_menu_Back);

            m_lasterror = "";

            m_ownerscreen = p_owner;
        }
        #endregion Constructor

        #region Update
        public override void Update(GameTime p_gametime)
        {
            base.Update(p_gametime);

            if (!m_rendersubscreen && m_ready)
            {
                // If mouse have moved, clear keyboard selection
                if (m_lastmouse.X != m_currentmouse.X || m_lastmouse.Y != m_currentmouse.Y)
                {
                    // Hit test all components (only menu items here anyway)
                    foreach (GameScreenComponent t_component in m_selectable_components)
                    {
                        t_component.HitTest(new Vector2(m_currentmouse.X, m_currentmouse.Y));
                    }
                }

                // Quick Match?
                if (m_menu_QuickMatch.Active && m_activatetrigger)
                    StartSubscreen(new ScreenQuickMatch(m_game, this));

                // LIVE Game?
                if (m_menu_LIVEMatch.Active && m_activatetrigger)
                    Console.WriteLine("LIVE Game not implemented!");
                    // TODO: LIVE game?

                // Back?
                if (m_menu_Back.Active && m_activatetrigger)
                    this.Close();
            }

            // Store keyboard and mouse states
            StoreStates();
        }
        #endregion Update

        #region ReEntry
        public override void ReEntry()
        {
            //m_lasterror = "";
            //m_showerror = false;
            if (m_lasterror != "")
            {
                int t_i = 0;
                foreach (Char t_char in m_lasterror)
                {
                    t_i++;
                    if (t_i % 50 == 0)
                    {
                        m_lasterror = m_lasterror.Insert(t_i, "\n");
                    }
                }
            }
            base.ReEntry();
        }
        #endregion ReEntry

        #region Draw
        public override void Draw()
        {
            if (!m_rendersubscreen)
            {
                m_spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, m_screenmatrix);

                // Draw menu components
                foreach (GameScreenComponent t_component in m_selectable_components)
                {
                    t_component.Draw(m_spriteBatch, false);
                }

                // Draw alpha disclaimer
                String t_disclaim = "Please note:\nCurrent build only support multiplayer games on\nlocal subnet using 'Quick Match' functionality.";
                m_spriteBatch.DrawString(m_smallfont, t_disclaim, new Vector2(300, 220), Color.White);

                // Draw last error if present!
                //m_lasterror = "Hello! Test test test taoeaoe aoeao eao eaoe taoe haoi ehtaotheahoteihtaodi ethdaioe iao a aaoe aoeado tehaote d";
                if (m_lasterror != "")
                {
                    m_spriteBatch.DrawString(m_smallfont, m_lasterror, new Vector2(300, 351), Color.Black);
                    m_spriteBatch.DrawString(m_smallfont, m_lasterror, new Vector2(300, 350), Color.Red);
                }

                m_spriteBatch.End();
            }
            else
            {
                m_subscreen.Draw();
            }

            base.Draw();
        }
        #endregion Draw

    }
}
